import { FormInstance } from 'antd';
import { forwardRef } from 'react';
import type { YmQueryFilterProps } from 'ym-design';
import { YmQueryFilter } from 'ym-design';
export interface TableSearchFormProps<FormValue = any> {
  searchFrom?: YmQueryFilterProps<FormValue> | false;
  onSubmit?: (newValus: Record<string, any>) => void;
  onReset?: () => void;
}

export const TableSearchForm = forwardRef<
  { getFormInstance: () => FormInstance },
  TableSearchFormProps
>((props, ref) => {
  const { searchFrom, onSubmit, onReset } = props;
  if (searchFrom && searchFrom.formItems?.length) {
    return (
      <YmQueryFilter
        ref={ref}
        {...searchFrom}
        submitter={{
          onSubmit: (values: Record<string, any>) => {
            searchFrom.submitter?.onSubmit?.(values);
            onSubmit?.(values);
          },
          onReset: () => {
            searchFrom.submitter?.onReset?.();
            onReset?.();
          },
        }}
      />
    );
  } else {
    return null;
  }
});
